package cn.bytecollege.admin.controller;

import cn.bytecollege.admin.service.ProductService;
import cn.bytecollege.common.entity.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ProductSave_FindProductNameController", value = "/admin/saveProduct_FindName")
public class ProductSave_FindProductNameController extends HttpServlet {
    private ProductService productService = null;
    public ProductSave_FindProductNameController(){
        productService = new ProductService();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String productName = request.getParameter("productName");
        Product tempProduct = productService.findByProductName(productName);
        int k=0;
        if (tempProduct != null){
            k=1; //k=1表示有用户名了。
        }
        //把k值写入到响应包，把k值返回给前台。
        response.getWriter().print(k);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
